package com.hbase.vo;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Properties;

public class Tables {

    private static final Logger logger = LoggerFactory.getLogger(Tables.class);

    public static final Properties properties = new Properties();
    //hbase namespace surh add  by 20180202
    private static String NAME_SPACE = "";

    private static TableName valueOf(String tableNameStr){
        return TableName.valueOf(NAME_SPACE.concat(tableNameStr));
    }

    static{
        try {
            loadHbaseConfig();
        } catch (Throwable t) {
//            LOG.error("Failure during static initialization", t);
            throw t;
        }
    }
    private static void loadHbaseConfig(){
        Properties properties = new Properties();
        InputStream is = readHbaseConfig();
        if(is == null){
            return;
        }
        try {
            properties.load(is);
            NAME_SPACE = properties.getProperty("hbase.namespace");
            if(NAME_SPACE == null || NAME_SPACE.trim().isEmpty()){
                NAME_SPACE ="";
            }
        } catch (IOException e) {
            logger.error("load hbase properties fail ",e);
        }finally{
            closeIs(is);
        }
    }

    private static InputStream readHbaseConfig(){
        String hbaseProperties = "hbase.properties";
        InputStream is = Tables.class.getResourceAsStream("/".concat(hbaseProperties));
        if(is != null){
            return is;
        }
        try{
            return new FileInputStream(new File("../classes/".concat(hbaseProperties)));
        }catch(FileNotFoundException e){
            logger.error("hbase properties file [{}] not found ，please check ",hbaseProperties,e.getMessage());
            return null;
        }
    }

    private static void closeIs(InputStream is){
        if(is == null){
            return ;
        }
        try {
            is.close();
        } catch (IOException e) {
        }
    }

    public static final TableName TEST  = valueOf("solr_test");
    public static final byte[] TEST_CF_C= Bytes.toBytes("Info");
    public static final byte[] TEST_C_NETWORK_TYPE= Bytes.toBytes("networkType");
    public static final byte[] TEST_C_SYSTEM= Bytes.toBytes("system");
    public static final byte[] TEST_C_VERSION= Bytes.toBytes("version");
    public static final byte[] TEST_C_CPU_USE_TIME= Bytes.toBytes("cpuUseTime");
    public static final byte[] TEST_C_CLICK_COUNT= Bytes.toBytes("clickCount");



}
